package com.bysj.controller;

import com.bysj.entity.Topic;
import com.bysj.entity.User;
import com.bysj.service.TopicService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    //添加贴子
    @ResponseBody
    @RequestMapping("/addTopic")
    public String addTopic(@RequestBody Topic topic){
        topicService.addTopic(topic);
        return "ok";
    }
    //查询所有的贴子
    @RequestMapping("/getTopicAll")
    @CrossOrigin
    public List<Topic> getTopicAll(Map map){
        return topicService.getTopicAll(map);
    }

    //根据id查询贴子
    @RequestMapping("/getTopicById")
    @CrossOrigin
    public List<Topic> getTopicById(Integer id){
        return topicService.getTopicById(id);
    }

    //根据用户id查询贴子
    @RequestMapping("/getTopicByUid")
    public List<Topic> getTopicByUid(Integer id){
        return topicService.getTopicByUid(id);
    }

    //根据贴子id删除贴子
    @ResponseBody
    @RequestMapping("/deleteTopicById")
    @CrossOrigin
    public String deleteTopicById(Integer id){
        topicService.deleteTopicById(id);
        return "success";
    }

    //根据输入的文字查询相关贴子
    @RequestMapping("/getTopicByWords")
    @CrossOrigin
    public List<Topic> getTopicByWords( String searchwords){
        return topicService.getTopicByWords(searchwords);
    }

    //根据贴子类别查询贴子
    @RequestMapping("/getTopicByCategoryName")
    public List<Topic> getTopicByCategoryName( String categoryname){
        return topicService.getTopicByCategoryName(categoryname);
    }

    //分页查询所有贴子
    @GetMapping("/getAllTopicByPage")
    @CrossOrigin
    public PageInfo<Topic> getAllTopicByPage(Map map,
                                           @RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize ){
        PageInfo<Topic> topicList = topicService.getAllTopicByPage(map,pageNum,pageSize);
        return topicList;


    }
}
